package com.ruoyi.common.security.feign;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.ip.IpUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * feign 请求拦截器
 * 
 * @author ruoyi
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor
{
    @Override
    public void apply(RequestTemplate requestTemplate)
    {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest();
        if(httpServletRequest == null){
            if(SecurityUtils.getOrgId() != null){
                requestTemplate.header(SecurityConstants.DETAILS_ORG_ID, SecurityUtils.getOrgId().toString());
            }
            if(SecurityUtils.getUserId() != null){
                requestTemplate.header(SecurityConstants.DETAILS_USER_ID, SecurityUtils.getUserId().toString());
            }
        }
        if (StringUtils.isNotNull(httpServletRequest))
        {
            Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
            // 传递用户信息请求头，防止丢失
            String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
            if (StringUtils.isNotEmpty(userId))
            {
                requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
            }
            String userKey = headers.get(SecurityConstants.USER_KEY);
            if (StringUtils.isNotEmpty(userKey))
            {
                requestTemplate.header(SecurityConstants.USER_KEY, userKey);
            }
            String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
            if (StringUtils.isNotEmpty(userName))
            {
                requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
            }
            String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
            if (StringUtils.isNotEmpty(authentication))
            {
                requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
            }
            String pageNum = ServletUtils.getParameter(TableSupport.PAGE_NUM);
            if(StringUtils.isNotEmpty(pageNum)) {//判断请求中是否包含页码，如果包含则将分页参数转发到提供端
                requestTemplate.query(TableSupport.PAGE_NUM, ServletUtils.getParameter(TableSupport.PAGE_NUM));
                requestTemplate.query(TableSupport.PAGE_SIZE, ServletUtils.getParameter(TableSupport.PAGE_SIZE));
                requestTemplate.query(TableSupport.ORDER_BY_COLUMN, ServletUtils.getParameter(TableSupport.ORDER_BY_COLUMN));
                requestTemplate.query(TableSupport.IS_ASC, ServletUtils.getParameter(TableSupport.IS_ASC));
                requestTemplate.query(TableSupport.REASONABLE, ServletUtils.getParameter(TableSupport.REASONABLE));
            }
            // 配置客户端IP
            requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr(ServletUtils.getRequest()));
        }
    }
}